package com.dchh.android.suverynew.act;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dchh.android.suverynew.R;
import com.dchh.android.suverynew.base.SBaseActivity;
import com.dchh.android.suverynew.common.SPreferencesManager;
import com.dchh.android.suverynew.views.MapPopupWindow;
import com.dhcc.android.base.base.MSApplication;
import com.dhcc.android.base.db.DBManagerUtil;
import com.dhcc.android.base.utils.CommenUtil;
import com.dhcc.android.base.utils.Constants;
import com.dhcc.android.base.utils.FileUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * 实割实测填报页面
 *
 * @author zhoudan
 * @date 2016年4月6日
 */
public class SGSCReportActivity extends SBaseActivity {

    private RelativeLayout main;
    private LinearLayout save;
    private RelativeLayout map_icon;

    private Map<String, String> data;

    private String root;
    private String taskName;
    private String dbPath;
    private String table = "实割实测表";

    private String YFBHU;
    private String DKBHU;
    private String ZWBH;
    private String ZWMC;

    private DBManagerUtil dbManagerUtil;

    private boolean isEdit = false;
    private boolean isNew = false;

    private TextView zwmc;
    private EditText ybfysl;
    private EditText ybmj;
    private EditText dkybmzhj;
    private EditText szhj;
    private EditText szgjbz;
    private EditText mksl;
    private EditText mnjhfyl;
    private EditText tdzk;
    private EditText ggsy;
    private EditText ggfs;
    private EditText zz;
    private EditText szcd;
    private EditText jgqk;
    private EditText jbqk;
    private EditText jsqk;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yield_sgsc_tianbao);
    }

    private String getTitleStr() {
        String s = Constants.selects.get(0).get("CUNMC")
                + "样方" + Constants.selects.get(1).get("YFBH")
                + "地块" + Constants.select.get("YFDKBH");

        return s;
    }

    @Override
    protected void setTitle(TextView toolbar_title) {
        toolbar_title.setText(getTitleStr());
    }

    @Override
    protected void initView() {
        root = SPreferencesManager.getValue(MSApplication.TASK_SAVE_PATH, FileUtil.getSavePath());
        taskName = getIntent().getStringExtra("taskName");
        YFBHU = getIntent().getStringExtra("YFBHU");
        DKBHU = Constants.select.get("DKBHU");
        ZWBH = Constants.zwMap.get("ZWBH");
        ZWMC = Constants.zwMap.get("ZWMC");
        dbPath = root + "/" + taskName + "/TASK/YIELD/DATA.db";
        dbManagerUtil = DBManagerUtil.getInstance(dbPath, this);
        zwmc = (TextView) main.findViewById(R.id.zwmc);
        ybfysl = (EditText) main.findViewById(R.id.ybfysl);
        ybmj = (EditText) main.findViewById(R.id.ybmj);
        dkybmzhj = (EditText) main.findViewById(R.id.dkybmzhj);
        szhj = (EditText) main.findViewById(R.id.szhj);
        szgjbz = (EditText) main.findViewById(R.id.szgjbz);
        mksl = (EditText) main.findViewById(R.id.mksl);
        mnjhfyl = (EditText) main.findViewById(R.id.mnjhfyl);
        tdzk = (EditText) main.findViewById(R.id.tdzk);
        ggsy = (EditText) main.findViewById(R.id.ggsy);
        ggfs = (EditText) main.findViewById(R.id.ggfs);
        zz = (EditText) main.findViewById(R.id.zz);
        szcd = (EditText) main.findViewById(R.id.szcd);
        jgqk = (EditText) main.findViewById(R.id.jgqk);
        jbqk = (EditText) main.findViewById(R.id.jbqk);
        jsqk = (EditText) main.findViewById(R.id.jsqk);
        save = (LinearLayout) main.findViewById(R.id.save_layout);
        getData();
        zwmc.setText(ZWMC);
        if (data != null) {
            ybfysl.setText(data.get("YBFYSL"));
            ybmj.setText(data.get("YBMJ"));
            dkybmzhj.setText(data.get("DKYBMZHJ"));
            szhj.setText(data.get("SZHJ"));
            szgjbz.setText(data.get("SZGJBZ"));
            mksl.setText(data.get("MKSL"));
            mnjhfyl.setText(data.get("MNJHFYL"));
            tdzk.setText(data.get("TDZK"));
            ggsy.setText(data.get("GGSY"));
            ggfs.setText(data.get("GGFS"));
            zz.setText(data.get("ZZ"));
            szcd.setText(data.get("SZCD"));
            jgqk.setText(data.get("JGQK"));
            jbqk.setText(data.get("JBQK"));
            jsqk.setText(data.get("JSQK"));
        } else {
            isNew = true;
            data = new HashMap<String, String>();
            data.put("YFBHU", YFBHU);
            data.put("DKBHU", DKBHU);
            data.put("ZWBH", ZWBH);
            data.put("ZWMC", ZWMC);
        }
        ybfysl.addTextChangedListener(new MyTextChange("YBFYSL"));
        ybmj.addTextChangedListener(new MyTextChange("YBMJ"));
        dkybmzhj.addTextChangedListener(new MyTextChange("DKYBMZHJ"));
        szhj.addTextChangedListener(new MyTextChange("SZHJ"));
        szgjbz.addTextChangedListener(new MyTextChange("SZGJBZ"));
        mksl.addTextChangedListener(new MyTextChange("MKSL"));
        mnjhfyl.addTextChangedListener(new MyTextChange("MNJHFYL"));
        tdzk.addTextChangedListener(new MyTextChange("TDZK"));
        ggsy.addTextChangedListener(new MyTextChange("GGSY"));
        ggfs.addTextChangedListener(new MyTextChange("GGFS"));
        zz.addTextChangedListener(new MyTextChange("ZZ"));
        szcd.addTextChangedListener(new MyTextChange("SZCD"));
        jgqk.addTextChangedListener(new MyTextChange("JGQK"));
        jbqk.addTextChangedListener(new MyTextChange("JBQK"));
        jsqk.addTextChangedListener(new MyTextChange("JSQK"));
        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isEdit) {
                    dbManagerUtil.sgscSave(data, table, dbPath, isNew);
                    isNew = false;
                }
                CommenUtil.toast("保存成功！", SGSCReportActivity.this);
            }
        });
    }

    private void getData() {
        data = dbManagerUtil.getSGSCData(table, dbPath, ZWBH, YFBHU, DKBHU);
    }


    public class MyTextChange implements TextWatcher {

        private String key;

        public MyTextChange(String key) {
            this.key = key;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            String val = s.toString().trim();
            data.put(key, val);

            isEdit = true;
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.sgscreport_select_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.map) {
            MapPopupWindow mapwindow = new MapPopupWindow(SGSCReportActivity.this);
            mapwindow.show(item.getActionView(), Constants.ZRDKBITMAP);
        }
        return true;
    }
}
